################################################################################
# Title   : Replication Code — Union Spillovers Paper
# Project : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
# Authors : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
# Output  : Figures 3
# Date    : August 2025
################################################################################

# Set default graphical theme
theme_set(theme_light())

# Create output directory if it doesn't exist
if (!dir.exists(figs)) dir.create(figs, recursive = TRUE)


df <- read_dta(paste0(wd,"/Trends_figure_dta.dta")) %>% 
  mutate(year = case_when(
    year2 ==  1 ~ "1980",
    year2 ==  2 ~ "1990",
    year2 ==  3 ~ "2000",
    year2 ==  4 ~ "2010",
    year2 == 5  ~ "2020")) %>% 
  mutate(year = as.Date(ISOdate(year, 1, 1)))

# Transform data into long format
data_long <- df %>%
  pivot_longer(
    cols = c(threat, uprem2, union_share, phi_tt), 
    names_to = "Category",      
    values_to = "Value" 
  ) 


# Plot the data
ggplot(data_long, aes(x = year, 
                      y = Value, 
                      color = Category, 
                      shape = Category, 
                      linetype = Category)) +
  geom_point(size=2) +
  geom_line(size=1) +
  labs(
    y = "Normalized Percentage Change",
    x = "Year"
  ) +
  scale_color_discrete(name = "Category", 
                       labels = c("Transitions",
                                  "Threat ",
                                  "Proportion Union",
                                  TeX("Union Premium")
                       ))+ 
  scale_linetype_discrete(name = "Category",
                          labels = c("Transitions",
                                     "Threat ",
                                     "Proportion Union",
                                     TeX("Union Premium")
                          ))+   
  scale_shape_manual(values = c("phi_tt" = 16, "threat" = 17, "union_share" = 1, "uprem2" = 2),
                     name = "Category", 
                     labels = c("Transitions",
                                "Threat ",
                                "Proportion Union",
                                TeX("Union Premium")
                     ))+ 
  scale_x_date(
    expand = c(0.01, 0.01), 
    date_labels = "%Y"
  ) +
  theme_minimal() +
  theme(
    legend.position = c(0, 0),  # Bottom-left corner
    legend.justification = c(0, 0),  # Align bottom-left
    legend.background = element_rect(fill = "white", color = "black"),  # Add background to legend
    legend.title = element_blank(),  # Remove legend title
    legend.text = element_text(size = 12),  # Adjust legend text size
    legend.key.width = unit(2, "lines"),
    legend.key.height = unit(1, "lines")
  ) +
  scale_y_continuous(limits = c(-80, 20)) +
  
  # Adjust the guides to have 2 rows and 2 columns
  guides(
    linetype = guide_legend(nrow = 4, ncol = 1, byrow = TRUE),
    colour = guide_legend(nrow = 4, ncol = 1, byrow = TRUE),
    shape = guide_legend(nrow = 4, ncol = , byrow = TRUE)
  )  -> p3

p3

ggsave(paste0(figs,"/Figure_3.pdf"),
       units = c("in"), width = 8, height = 4.5)